//user.js
let Models = require("../models");
var moment = require("moment");
var sign_period = require("../config/sign_period.js");

module.exports = {
  findAll: function (request, reply) {
    return Models.emp_sign.findAll({}).then(function (result) {
      return result || [];
    });
  },
  todaySignStatusList: function () {
    var today_str = moment().format("YYYY-MM-DD");
    console.log(today_str);

    return Models.sequelize
      .query(
        `
        SELECT 
          a.emp_id,
          a.emp_name,
          b.sign_one,
          b.sign_two,
          b.sign_three,
          b.sign_four,
          b.sign_date
        FROM employee as a
        left join (
          SELECT * FROM emp_sign   WHERE  (sign_date='${today_str}' || ISNULL(sign_date))
        ) as b
        on a.emp_id=b.emp_id
        AND emp_flag=1
        ORDER BY emp_id ASC
       `,
        {
          type: Models.sequelize.QueryTypes.SELECT
        }
      )
      .then(result => {
        return result || [];
        // 我们不需要在这里延伸，因为只有结果将返回给选择查询
      });
  },
  empSigning: function (request, reply) {

    let reponseMess = {
      code: 0,
      message: '操作失败',
      data: null
    };

    var emp_id = request.payload.emp_id || "";  //要打卡的人
    var emp_num = request.payload.emp_num || ""; //打卡第几次   1 早上班  2上午下班  3下行上班  4 下班   -1 员工自助打卡
    var emp_type = request.payload.emp_type || ""; //打卡类型    1  管理员补卡(补正常上下班时间)   2 管理员更新打卡(现在时间)
    var sign_date = request.payload.sign_date || moment().format("YYYY-MM-DD");//相天打当天

    console.log(emp_num)

    var emp_mod = null;

    if (emp_id == "") {
      reponseMess.message = "emp_id参数缺失";
      return reponseMess;
    }
    if (emp_num == "") {
      reponseMess.message = "emp_num参数缺失";
      return reponseMess;
    }

    return Models.employee.findById(emp_id).then((u) => {
      return u;
    }).then((u) => {

      if (u == null) {
        reponseMess.message = "员工未找到";
        return reponseMess;
      }
      emp_mod = u;
      return u;
    }).then((u) => {
      return Models.emp_sign.findAll({
        where: {
          'emp_id': emp_id,
          'sign_date': sign_date
        }
      })
    }).then((result) => {
      // console.log("======找到我了吗222======");
      // console.log(result);

      if (result.length > 0) {
        return result[0];
      }

      var emp_sign_mod = {
        emp_id: emp_mod.emp_id,
        emp_name: emp_mod.emp_name,
        id: null,
        sign_one: null,
        sign_two: null,
        sign_three: null,
        sign_four: null,
        sign_date: sign_date
      };
      return Models.emp_sign.create(emp_sign_mod).then(function (result) {
        return result;
      })
    }).then((result) => {
      // console.log("======我来了======");
      // console.log(result);

      var update_config = {

      }

      var toTime = moment().format("YYYY-MM-DD HH:mm:ss");


// console.log("===result.sign_one==")
// console.log(result.sign_one);

      //管理员才传这个


      //员工打卡  不传时间过来
      if (emp_num == "-1"||emp_num == -1) {


        if (result.sign_one.toString() == "Invalid date") {
         
          update_config["sign_one"] = toTime; //当时时间

        } else if (result.sign_two.toString() == "Invalid date") {

          update_config["sign_two"] = toTime; //当时时间

        } else if (result.sign_three.toString() == "Invalid date") {

          update_config["sign_three"] = toTime; //当时时间

        } else if (result.sign_four.toString() == "Invalid date") {

          update_config["sign_four"] = toTime; //当时时间

        }
      } else if (emp_num == "1") {
        //补卡直接为
        if (emp_type == "1") {
          toTime = moment(sign_date).format("YYYY-MM-DD") + " " + sign_period.one + ":00"
        }
        update_config["sign_one"] = toTime;
      } else if (emp_num == "2") {
        //补卡直接为
        if (emp_type == "1") {
          toTime = moment(sign_date).format("YYYY-MM-DD") + " " + sign_period.two + ":00"
        }
        update_config["sign_two"] = toTime;
      } else if (emp_num == "3") {
        //补卡直接为
        if (emp_type == "1") {
          toTime = moment(sign_date).format("YYYY-MM-DD") + " " + sign_period.three + ":00"
        }
        update_config["sign_three"] = toTime;
      } else if (emp_num == "4") {
        //补卡直接为
        if (emp_type == "1") {
          toTime = moment(sign_date).format("YYYY-MM-DD") + " " + sign_period.four + ":00"
        }
        update_config["sign_four"] = toTime;
      }




      console.log(update_config);

      return result.update(update_config);

    }).then((result) => {
      reponseMess = {
        code: 1000,
        message: '操作成功'
      }
      return reponseMess;
    })


  },
  saveOrUpdate: function (request, reply) {
    var user = {
      emp_id: 111,
      emp_name: "王孟贵2",
      emp_flag: 1,
      id: null,
      sign_one: "2018-05-17T09:00:00.000Z",
      sign_two: "2018-05-17T09:00:00.000Z",
      sign_three: "2018-05-17T09:00:00.000Z",
      sign_four: "2018-05-17T09:00:00.000Z",
      sign_date: "2018-05-16"
    };
    return Models.emp_sign.create(user).then(function (result) {
      return result || [];
    });
  }
};
